import { Op } from 'sequelize'
import { Income } from '../model/income'
import { CommonUtil } from './../utils/commonUtil'
export class IncomeDao {
  queryIncomeList(params: any): Promise<Array<Income>> {
    return Income.findAll({
      where: {
        date: {
          [Op.substring]: CommonUtil.getDateStr(new Date(params.date))
        },
        creator: params.creator
      },
      limit: params.limit,
      offset: params.offset
    })
  }
  count(params: any): Promise<number> {
    return Income.count({
      where: {
        date: {
          [Op.substring]: CommonUtil.getDateStr(new Date(params.date))
        },
        creator: params.creator
      }
    })
  }
  queryIncomeListByDateRange(params: any): Promise<Array<Income>> {
    const startDate = new Date(params.startDate)
    const endDate = new Date(params.endDate)
    return Income.findAll({
      where: {
        date: {
          [Op.gte]: startDate,
          [Op.lt]: endDate
        },
        creator: params.creator
      }
    })
  }
  upsertIncome(params: any): Promise<any> {
    return Income.upsert(params)
  }
  deleteIncome(params: any): Promise<number> {
    return Income.destroy({ where: params })
  }
}
